package com.dykj.fireFighting.web.app;

import com.dykj.fireFighting.api.base.bigdata.device.entity.Device;
import com.dykj.fireFighting.api.base.bigdata.device.service.IDeviceService;
import com.dykj.fireFighting.api.base.enterprise.entity.Enterprise;
import com.dykj.fireFighting.api.base.enterprise.service.IEnterpriseService;
import com.dykj.fireFighting.api.elasticsearch.devHis.entity.EsDevHis;
import com.dykj.fireFighting.api.elasticsearch.devHis.service.EsDevHisService;
import com.dykj.fireFighting.api.fire.devhis.dto.DevHisDto;
import com.dykj.fireFighting.api.fire.devhis.dto.DeviceDto;

import com.dykj.fireFighting.api.fire.devhis.entity.vo.DevHisWaterVo;
import com.dykj.fireFighting.api.fire.devhis.service.IDevHisService;

import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author fgc
 * @Date 2021/8/30 16:11
 * @ Description：维保端社会单位运行记录
 */
@Slf4j
@Api(value = "维保端社会单位运行记录接口", tags = {"维保端社会单位运行记录接口"})
@RestController
@RequestMapping("app/devHis")
public class AppDevHisController {
    @DubboReference
    private IEnterpriseService enterpriseService;
    @DubboReference
    private IDevHisService devHisService;
    @DubboReference
    private IDeviceService deviceService;
    @DubboReference
    private EsDevHisService esDevHisService;

    /**
     * 查看单位设备运行记录
     * @param devHisDto
     * @return
     */
    @AutoLog(value = "-查看单位设备运行记录")
    @ApiOperation(value = "查看单位设备运行记录", notes = "查看单位设备运行记录")
    @GetMapping("/getDevHis")
    public CommonResult getDevHis(DevHisDto devHisDto){
        if(StringUtils.isNotBlank(devHisDto.getSociologyDept())){
            List<String> unitIds= new ArrayList();
            unitIds.add(devHisDto.getSociologyDept());
            devHisDto.setUnitIds(unitIds);
        }else{
            HashMap<String, Object> params = new HashMap<>(16);
            params.put("weibaoUnit",ShiroUtil.getUser().getEnterpriseId());
            List<Enterprise> list=enterpriseService.querySocialUnits(params);
            List<String> unitIds= new ArrayList();
            for (Enterprise e: list) {
                unitIds.add(e.getId().toString());
            }
            devHisDto.setUnitIds(unitIds);
        }
        PageVo<EsDevHis> pageVo = esDevHisService.wbSearchOther(devHisDto.getUnitIds(), (int) devHisDto.getPage(), (int) devHisDto.getLimit());
        for (int i = 0; i < pageVo.getList().size(); i++) {
            Device device=deviceService.getDeviceByRelationId(pageVo.getList().get(i).getRelationId());
            if(null!=device){
                pageVo.getList().get(i).setDevType(device.getTypeName());
            }
        }
        return CommonResult.success(pageVo);
    }

    /**
     * 查看单位设备故障运行记录
     * @param devHisDto
     * @return
     */
    @AutoLog(value = "-查看单位设备故障运行记录")
    @ApiOperation(value = "查看单位设备故障运行记录", notes = "查看单位设备故障运行记录")
    @GetMapping("/getDevHisFault")
    public CommonResult getDevHisFaultList(DevHisDto devHisDto){
        if(StringUtils.isNotBlank(devHisDto.getSociologyDept())){
            List<String> unitIds= new ArrayList();
            unitIds.add(devHisDto.getSociologyDept());
            devHisDto.setUnitIds(unitIds);
        }else{
            HashMap<String, Object> params = new HashMap<>(16);
            params.put("weibaoUnit",ShiroUtil.getUser().getEnterpriseId());
            List<Enterprise> list=enterpriseService.querySocialUnits(params);
            List<String> unitIds= new ArrayList();
            for (Enterprise e: list) {
                unitIds.add(e.getId().toString());
            }
            devHisDto.setUnitIds(unitIds);
        }
        PageVo<EsDevHis> pageVo = esDevHisService.wbSearchGz(devHisDto.getUnitIds(), devHisDto.getWbRead(), (int) devHisDto.getPage(), (int) devHisDto.getLimit());
        for (int i = 0; i < pageVo.getList().size(); i++) {
            Device device=deviceService.getDeviceByRelationId(pageVo.getList().get(i).getRelationId());
            if(null!=device){
                pageVo.getList().get(i).setDevType(device.getTypeName());
            }
        }
        return CommonResult.success(pageVo);
    }
    /**
     * 查看单位设备故障运行记录未读数量
     * @param
     * @return
     */
    @AutoLog(value = "-查看单位设备故障运行记录未读数量")
    @ApiOperation(value = "查看单位设备故障运行记录未读数量", notes = "查看单位设备故障运行记录未读数量")
    @GetMapping("/getNotReadNum")
    public CommonResult getFaultNotRead(DevHisDto devHisDto){
        HashMap<String, Object> params = new HashMap<>(16);
        params.put("weibaoUnit",ShiroUtil.getUser().getEnterpriseId());
        List<Enterprise> list=enterpriseService.querySocialUnits(params);
        List<String> unitIds= new ArrayList();
        for (Enterprise e: list) {
            unitIds.add(e.getId().toString());
        }
        PageVo<EsDevHis> pageVo = esDevHisService.wbSearchGz(unitIds, "0", 1, 10);
        return CommonResult.success(pageVo.getTotalCount());
    }
    /**
     * 查看社会单位设备列表
     * @param
     * @return
     */
    @AutoLog(value = "-查看社会单位设备列表")
    @ApiOperation(value = "查看社会单位设备列表", notes = "查看社会单位设备列表")
    @GetMapping("/getDevicePage")
    public CommonResult getDevicePage(DeviceDto deviceDto){
        if(StringUtils.isNotBlank(deviceDto.getUnitId())){
            List<String> unitIds= new ArrayList();
            unitIds.add(deviceDto.getUnitId());
            deviceDto.setUnitIds(unitIds);
        }else{
            HashMap<String, Object> params = new HashMap<>(16);
            params.put("weibaoUnit",ShiroUtil.getUser().getEnterpriseId());
            List<Enterprise> list=enterpriseService.querySocialUnits(params);
            List<String> unitIds= new ArrayList();
            for (Enterprise e: list) {
                unitIds.add(e.getId().toString());
            }
            deviceDto.setUnitIds(unitIds);
        }

        return CommonResult.success(devHisService.pageList(deviceDto));
    }
    /**
     * 获取设备管理-智能设备详情
     */
    @AutoLog(value = "设备管理-智能设备-获取设备管理-智能设备详情")
    @ApiOperation(value = "获取设备管理-智能设备详情", notes = "获取设备管理-智能设备详情")
    @ApiImplicitParam(paramType = "path", name = "relationId", dataType = "String", required = true)
    @GetMapping("/details/{relationId}")
    /**
     *   deviceService引的社会端的，改 一系列复制到base，
     */
    public CommonResult<Object> details(@PathVariable("relationId") String relationId){
        Object device = deviceService.getDevicedetails(relationId);
        return CommonResult.success(device);
    }
    /**
     * 获取水设备运行记录
     */
    @AutoLog(value = "获取水设备运行记录")
    @ApiOperation(value = "获取水设备运行记录", notes = "获取水设备运行记录")
    @GetMapping("/devHisWater")
    public CommonResult getDevHisWater(@RequestParam("page")long page,@RequestParam("limit") long limit,@RequestParam("relationId") String relationId){
        PageVo<DevHisWaterVo> devHisWater = devHisService.getDevHisWater(page, limit, relationId);
        return CommonResult.success(devHisWater);
    }
}
